package com.dinzeer.sjapadder.sa;

import com.dinzeer.sjapadder.Config;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.capability.concentrationrank.ConcentrationRankCapabilityProvider;
import mods.flammpfeil.slashblade.entity.EntityJudgementCut;
import mods.flammpfeil.slashblade.entity.EntitySlashEffect;
import mods.flammpfeil.slashblade.util.KnockBacks;
import mods.flammpfeil.slashblade.util.VectorHelper;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;

public class BurningDance {

    public static void doBurningDance(LivingEntity entityIn) {
        // 执行8个方向的圆形斩击
        doCircleSlash(entityIn, -30.0F, 180.0F);
        doCircleSlash(entityIn, -30.0F, 90.0F);
        doCircleSlash(entityIn, -30.0F, 0.0F);
        doCircleSlash(entityIn, -30.0F, -90.0F);
        doCircleSlash(entityIn, 30.0F, 180.0F);
        doCircleSlash(entityIn, 30.0F, 90.0F);
        doCircleSlash(entityIn, 30.0F, 0.0F);
        doCircleSlash(entityIn, 30.0F, -90.0F);
        
        // 召唤12个次元斩
        summonJudgementCut(entityIn);
    }
    
    // 以玩家为中心生成12个均匀分布的次元斩
    public static void summonJudgementCut(LivingEntity playerIn) {
        double radius = 5.0; // 半径5
        int count = Config.countBurningDanceJudgementCut;
        
        for (int i = 0; i < count; i++) {
            // 计算角度（0-360度均匀分布）
            double angle = Math.toRadians(i * 360.0f / count);
            // 计算坐标
            double x = playerIn.getX() + radius * Math.cos(angle);
            double y = playerIn.getY() + playerIn.getEyeHeight() * 0.75F;
            double z = playerIn.getZ() + radius * Math.sin(angle);
            
            // 创建次元斩实体
            EntityJudgementCut judgementCut = new EntityJudgementCut(
                SlashBlade.RegistryEvents.JudgementCut, 
                playerIn.level()
            );
            judgementCut.setPos(x, y, z);
            judgementCut.setIsCritical(true);
            judgementCut.setDamage(1);
            judgementCut.setColor(16711680); // 红色
            judgementCut.setOwner(playerIn);
            playerIn.level().addFreshEntity(judgementCut);
        }
    }

    private static void spawnParticle(ParticleOptions type, LivingEntity player, int num, double rate) {
        Level world = player.level();
        RandomSource random = player.getRandom();

        for(int i = 0; i < num && !player.level().isClientSide(); ++i) {
            double xDist = (double)(random.nextFloat() * 2.0F - 1.0F);
            double yDist = (double)(random.nextFloat() * 2.0F - 1.0F);
            double zDist = (double)(random.nextFloat() * 2.0F - 1.0F);
            if (!(xDist * xDist + yDist * yDist + zDist * zDist > (double)1.0F)) {
                double x = player.getX() + ((random.nextDouble() * (double)2.0F - (double)1.0F) * (double)player.getBbWidth() - random.nextGaussian() * 0.02 * (double)10.0F) * rate;
                double y = player.getY();
                double z = player.getZ() + ((random.nextDouble() * (double)2.0F - (double)1.0F) * (double)player.getBbWidth() - random.nextGaussian() * 0.02 * (double)10.0F) * rate;
                ((ServerLevel)world).sendParticles(type, x, y, z, 0, xDist, yDist + 0.2, zDist, (double)1.0F);
            }
        }

    }

    public static void doCircleSlash(LivingEntity living, float roll, float yRot) {
        if (!living.level().isClientSide()) {
            spawnParticle(ParticleTypes.SMALL_FLAME, living, 25, 2.0F);
            
            // 计算特效位置：
            // 1. 获取玩家当前位置
            // 2. 添加眼睛高度的75%作为Y轴偏移
            // 3. 添加视线方向0.3格的偏移
            Vec3 pos = living.position()
                .add(0.0F, (double)living.getEyeHeight() * 0.75F, 0.0F)
                .add(living.getLookAngle().scale(0.3F));
            
            // 添加基于玩家朝向的额外偏移
            pos = pos
                .add(VectorHelper.getVectorForRotation(-90.0F, living.getViewYRot(0.0F)).scale(Vec3.ZERO.y))
                .add(VectorHelper.getVectorForRotation(0.0F, living.getViewYRot(0.0F) + 90.0F).scale(Vec3.ZERO.z))
                .add(living.getLookAngle().scale(Vec3.ZERO.z));
            
            // 创建斩击特效
            EntitySlashEffect jc = new EntitySlashEffect(SlashBlade.RegistryEvents.SlashEffect, living.level()) {
                public SoundEvent getSlashSound() {
                    return SoundEvents.EMPTY;
                }
            };
            jc.setPos(pos.x, pos.y, pos.z);
            jc.setOwner(living);
            jc.setRotationRoll(roll);
            jc.setYRot(living.getYRot() - 22.5F + yRot);
            jc.setXRot(0.0F);
            jc.setColor(16711680);
            jc.setMute(false);
            jc.setIsCritical(true);
            jc.setDamage((double)0.3F);
            jc.setKnockBack(KnockBacks.cancel);
            if (living != null) {
                living.getCapability(ConcentrationRankCapabilityProvider.RANK_POINT).ifPresent((rank) -> jc.setRank(rank.getRankLevel(living.level().getGameTime())));
            }

            living.level().addFreshEntity(jc);
        }
    }
}
